Repository navigation
fix(flags): don't match person_metadata filters against person properties - #1288
srednoff888-art wants to merge 4 commits into
Conversation
…ties A cohort can filter on Person metadata > First seen (type "person_metadata", key "created_at"). /flags reads that value from the persons table and ignores any created_at person property. Local evaluation treated every filter type other than cohort and flag as a person property, so a caller passing created_at got an answer based on that value, which could disagree with /flags. Only person and group filters are now matched against the properties the caller passes. Any other type is unsupported_definition: the definition check flags it when definitions load, and evaluation falls back to /flags (or stays unresolved with only_evaluate_locally). match_property itself is unchanged. Part of PostHog/posthog#137503 (posthog-python) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
The runtime change in posthog/feature_flags.py looks minimal and matches both /flags (person_metadata reads from the persons table rather than person properties) and posthog-dotnet's allow-list approach for unsupported property filter types.
There are two test concerns inline before approval:
- The new
_is_unsupported_propertycheck (posthog/feature_flags.py:1862) has no test coverage, and the new test file only exercises the deprecatedget_feature_flagpath. TestPersonMetadataFilters._clientconstructs liveClient(..., send=True)instances withoutclient.shutdown(), leaking backgroundConsumerthreads that attempt outbound capture POSTs during longer test runs.
Outside the scope of this PR (non-blocking follow-ups): codifying the property filter type allow-list in PostHog/sdk-specs, aligning remaining peer server SDKs and the posthog.com local-evaluation restrictions docs, and evaluating separately whether sdk-specs should address multi-condition multivariate / early_exit catch-all fallthrough after an inconclusive condition.
AI-assisted review.
The error let later conditions decide the flag. Only /flags knows the value, so treat it like a static cohort. Also cover the load-time warning, evaluate_flags and negation, with a client that sends nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
Thanks for updating the tests and fixing the test client cleanup — both concerns from the earlier review (_load_feature_flags() / evaluate_flags() / unresolved_flags / negation & negative-control coverage, and send=False + client.shutdown()) are addressed cleanly.
One Breaking regression remains inline on posthog/feature_flags.py before approval: switching _assert_property_filter_type_supported to raise RequiresServerEvaluation breaks three-valued AND/OR short-circuiting in match_property_group for dynamic cohorts that combine person_metadata with another filter (affecting both AND — including not_in — and OR cohorts, regressing merge-base behavior when created_at is omitted, and diverging from sdk-specs).
Optional Suggestion (non-blocking, outside the current diff): consider adding "an unsupported filter type" to the example list in the load-time unsupported_definition warning (posthog/client.py:4280-4288) and UnresolvedFlagReason.UNSUPPORTED_DEFINITION docstring (posthog/types.py:130-133) so operators seeing the warning for a dynamic cohort with a First seen filter can diagnose it directly.
(Outside this PR's scope as non-blocking follow-ups: pre-existing filter-order handling in _match_condition_properties at posthog/feature_flags.py:928 matches test_static_cohort_after_unchecked_filter_lets_later_conditions_resolve and peer SDKs; documenting person_metadata restrictions in PostHog/posthog.com and codifying unsupported filter types in PostHog/sdk-specs.)
AI-assisted review.
A cohort stopped at the first filter that only the server can check, even when another filter had already decided it. Raise the error only when the cohort stays undecided. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
Thanks for the fast follow-up in 07ee9f6 — the _RequiresServerFilter deferral cleanly resolves the dynamic-cohort three-valued AND/OR regression (all 9 decisive-sibling regression cases now pass on 07ee9f6, alongside all 21 new tests and 1530 flag tests), and the warning/docstring updates look great.
One narrow Concern remains inline on posthog/feature_flags.py before approval: deferring _RequiresServerFilter in match_property_group drops higher-precedence UnresolvedFlagReason values (UNSUPPORTED_DEFINITION lost to UNRESOLVED_DEPENDENCY), with the same precedence drop amplified at _raise_condition_errors:960-961 and _match_flag_conditions:633-641. There is also one optional Suggestion inline on posthog/test/test_feature_flags_person_metadata.py.
(Outside this PR's scope as non-blocking follow-ups: documenting person_metadata / unsupported filter types in PostHog/posthog.com's local-evaluation restrictions, codifying them in PostHog/sdk-specs, and aligning peer server SDKs under PostHog/posthog#137503.)
AI-assisted review.
…lter An error that only the server can resolve kept its own reason, so a flag with a runtime unsupported_definition filter could be reported as unresolved_dependency. The reasons are now merged in the cohort group, in the condition and across conditions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SDK performance: posthog-pythonBenchmarks completed. No changes flagged for review. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
|
lemme confirm PostHog/sdk-specs#251 before merging |
💡 Motivation and Context
Part of PostHog/posthog#137503, for posthog-python, where the issue was reproduced.
A cohort can filter on Person metadata > First seen:
{"type": "person_metadata", "key": "created_at", ...}./flagsreads that value from the persons table and ignores anycreated_atperson property. Local evaluation treated every filter type other thancohortandflagas a person property, so:person_properties={}was inconclusive and fell back to/flags(correct);person_properties={"created_at": "2026-06-01"}returned a local answer from that value, which can disagree with/flagsfor teams that set their owncreated_atperson property.Following the fix the issue suggests (and what posthog-dotnet already does), a filter type that local evaluation can't evaluate is unsupported:
_match_condition_filternow matches onlypersonandgroupfilters (or one without atype) against the caller's properties.cohortandflagfilters keep their own matching. Any other type raises_RequiresServerFilter, a privateRequiresServerEvaluationwithunsupported_definition, so a later condition can't decide the flag. This covers flag conditions and cohort criteria, which both go through it.match_property_groupholds that error back while another filter can still decide the cohort (ANDwith a mismatch,ORwith a match), as the spec's three-valued composition requires, and raises it when the cohort stays undecided. Missing cohorts and malformed groups still raise at once._is_unsupported_propertyapplies the same rule, so the definition check marks such a flagunsupported_definitionwhen definitions load.UNSUPPORTED_DEFINITIONdocstring mention filter types.match_propertyis unchanged.The same change for posthog-node is PostHog/posthog-js#5840.
💚 How did you test it?
New
posthog/test/test_feature_flags_person_metadata.py, throughClient.evaluate_flags:person_metadatafilter in a cohort, caller passescreated_at/flagsand returns its answerperson_metadatafilter directly in a flag condition/flagsonly_evaluate_locally=True, with and withoutcreated_at, and with negation ornot_inunsupported_definition, no/flagscallunsupported_definitionwarningearly_exitexclusion, or a dependent flag/flagsFalselocallyANDorORcohort, also withnot_in, in either order, with the filter nested, in another cohort or behind a flag dependency/flagscall/flagspersonor untyped filter oncreated_at, or agroupfilterReverting any one part of the change (the error type, the deferral, the dependency's error type, the definition check) makes some of these fail.
posthog/testtree with thetestextra, on Windows: 8938 passed, 41 skipped, 16 failed. 14 of them fail on the base commit too:forkhooks, path separators, and a digest of vendored fixtures. The other two are timing-dependent here: one also fails without this commit, the other passes when run alone.Posthogclient against a local HTTP server that serves the definitions and/flags, with anANDcohort of a plan filter and First seen. It answers locally for a person the plan rules out, and makes exactly one/flagsrequest otherwise.ruff format --checkis clean.ruff checkreports nothing in the new test file and nothing new infeature_flags.py.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
19000d3.person,group) instead of special-casingperson_metadata, so a filter type added on the server later also falls back instead of being matched as a person property.🤖 Generated with Claude Code